package com.ruoyi.web.controller.business;

import cn.afterturn.easypoi.word.WordExportUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system/operation")
public class TOperationController extends BaseController {
    @Autowired
    TOperationService tOperationService;

    private String prefix = "system/operation";

    @RequiresPermissions("system:operation:view")
    @GetMapping()
    public String operation() {
        return prefix + "/operation";
    }

    /**
     * 查詢銷售額
     *
     * @param
     * @return
     */
    @RequiresPermissions("system:operation:getSalesData")
    @PostMapping("/getSalesData")
    @ResponseBody
    public TableDataInfo getSalesData(TOperationQuery tOperationQuery) {
        startPage();
        if (tOperationQuery.getDrugId() != null) {
            tOperationQuery.setDrugId("'" + tOperationQuery.getDrugId().replace(",", "','") + "'");
        }
        List<TDoctor> data = tOperationService.getDoctorSalesData(tOperationQuery);
        return getDataTable(data);
    }

    /**
     * 日报
     */
    @RequiresPermissions("system:operation:getSalesData")
    @PostMapping("/getDaily")
    @ResponseBody
    public AjaxResult getDaily(TOperationQuery tOperationQuery) {
        String startDate = tOperationQuery.getStartDate();
        String endDate = tOperationQuery.getEndDate();

        Map<String, Object> map = new HashMap<String, Object>();
        //总营业额
        int amount = tOperationService.getDailyBusiness(tOperationQuery);
        //总退款额
        int refund = tOperationService.getDailyRefund(tOperationQuery);
        //总进货额
        int purchaseAmount = tOperationService.getPurchaseAmount(tOperationQuery);
        //营业额列表
        List<Map<String,Object>> businessList = tOperationService.getBusinessList(tOperationQuery);
        //进货列表
        List<Map<String, Object>> purchaseList = tOperationService.getPurchaseList(tOperationQuery);
        //销售列表
        List<Map<String, Object>> saleList = tOperationService.getSaleList(tOperationQuery);
        Date date=new Date();
        SimpleDateFormat dateFormat=new SimpleDateFormat("YYYY-MM-dd");
        String format = dateFormat.format(date);
        String time;
        if (endDate != null && endDate.length() != 0 && startDate != null && startDate.length() != 0){
            //有时间参数
            time = startDate + "至" +endDate;

        }else {
            time = format;
        }

        map.put("amount",amount);
        map.put("refund",refund);
        map.put("purchaseAmount",purchaseAmount);
        map.put("businessList",businessList);
        map.put("purchaseList",purchaseList);
        map.put("saleList",saleList);
        map.put("date",format);
        map.put("time",time);
//D:\报表
        try {
            XWPFDocument doc = WordExportUtil.exportWord07(
                    "D:\\报表\\大连甘井子新玉胜路诊所日报.docx", map);
            FileOutputStream fos = new FileOutputStream("D:\\报表\\日报-"+time+".docx");
            doc.write(fos);
            fos.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return toAjax(1);
    }


    /**
     * 月报
     */
    @RequiresPermissions("system:operation:getSalesData")
    @PostMapping("/getMonthlyReport")
    @ResponseBody
    public AjaxResult getMonthlyReport(TOperationQuery tOperationQuery) {
        String startDate = tOperationQuery.getStartDate();
        String endDate = tOperationQuery.getEndDate();

        Map<String, Object> map = new HashMap<String, Object>();
        //总销售额
        int amount = tOperationService.getMonthBusiness(tOperationQuery);
        //总利润
        int profit = tOperationService.getProfit(tOperationQuery);
        //总进货额
        int purchaseAmount = tOperationService.getMonthPurchaseAmount(tOperationQuery);
        //进价总额
        int totalPurchasePrice = tOperationService.getTotalPurchasePrice();
        //售价总额
        int totalSellingPrice = tOperationService.getTotalSellingPrice();
        //营业额列表
        List<Map<String,Object>> businessList = tOperationService.getMonthBusinessList(tOperationQuery);
        Date date=new Date();
        SimpleDateFormat dateFormat=new SimpleDateFormat("YYYY-MM");
        String format = dateFormat.format(date);

        String time;
        if (endDate != null && endDate.length() != 0 && startDate != null && startDate.length() != 0){
            //有时间参数
            time = startDate + "至" +endDate;
        }else {
            time = format;
        }

        map.put("amount",amount);
        map.put("purchaseAmount",purchaseAmount);
        map.put("businessList",businessList);
        map.put("date",format);
        map.put("time",time);
        map.put("profit",profit);
        map.put("totalPurchasePrice",totalPurchasePrice);
        map.put("totalSellingPrice",totalSellingPrice);

        try {
            XWPFDocument doc = WordExportUtil.exportWord07(
                    "D:\\报表\\大连甘井子新玉胜路诊所月报.docx", map);
            FileOutputStream fos = new FileOutputStream("D:\\报表\\月报-"+time+".docx");
            doc.write(fos);
            fos.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return toAjax(1);
    }

}
